package com.airticket.demo.controller;

import com.airticket.demo.bean.*;
import com.airticket.demo.bean.result.AirlineMap;
import com.airticket.demo.bean.result.AirlineSimple;
import com.airticket.demo.bean.result.AirlinesMapCount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 中间视图渲染数据接口
 */
@RestController
public class MiddleViewController {

    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 出进城航班量
     * @return
     */
    @GetMapping("/line/getAll")
    public JsonResult<AirlinesMapCount> getMapCount()
    {
        //返回数据
        JsonResult<AirlinesMapCount> jsonResult;
        //查询出入航班信息
        List<OutLines> outLines=mongoTemplate.findAll(OutLines.class);
        List<InputLines> inputLines=mongoTemplate.findAll(InputLines.class);
        //查询结果验证
        if(outLines!=null && outLines.size()>0 &inputLines!=null && inputLines.size()>0)
        {
            AirlinesMapCount airlinesMapCount=new AirlinesMapCount(outLines.get(0).getCount(),inputLines.get(0).getCount());
            //查询数据封装
            jsonResult=new JsonResult<>(true,"查询成功",airlinesMapCount);
        }
        else
        {
            jsonResult=new JsonResult<>(false,"查询失败");
        }
        return jsonResult;
    }

    /**
     * 航线地图
     * @return
     */
    @GetMapping("/map/getLines")
    public JsonResult<AirlineMap> getAirlineMap()
    {
        //返回数据
        JsonResult<AirlineMap> airlineMapJsonResult;
        //数据库查询，查询所有从成都出发与达到成都的航班信息
        List<ArrCity_Flow> arrCityFlows =mongoTemplate.findAll(ArrCity_Flow.class,"ArrCity_Flow");
        List<DepCity_Flow> depCityFlows =mongoTemplate.findAll(DepCity_Flow.class,"DepCity_Flow");
        //查询结果验证
        if(arrCityFlows !=null && arrCityFlows.size()>0 && depCityFlows !=null && depCityFlows.size()>0)
        {
            //成都到其他城市
            List<AirlineSimple> fromChengdu=new ArrayList<>();
            for(ArrCity_Flow arrCityFlow : arrCityFlows)
            {
                AirlineSimple airlineSimple=new AirlineSimple("成都", arrCityFlow.getArrCityName(), arrCityFlow.getCount());
                fromChengdu.add(airlineSimple);
            }
            //其他城市到达成都
            List<AirlineSimple> toChengdu=new ArrayList<>();
            for(DepCity_Flow depCityFlow : depCityFlows)
            {
                AirlineSimple airlineSimple=new AirlineSimple(depCityFlow.getDepCityName(),"成都", depCityFlow.getCount());
                toChengdu.add(airlineSimple);
            }
            AirlineMap airlineMap=new AirlineMap();
            airlineMap.setFromChengDu(fromChengdu);
            airlineMap.setToChengDu(toChengdu);
            //返回数据封装
            airlineMapJsonResult=new JsonResult<>(true,"查询成功",airlineMap);
        }
        else
        {
            airlineMapJsonResult=new JsonResult<>(false,"查询失败，请重新操作");
        }
        return airlineMapJsonResult;
    }



}
